﻿using System.Xml.Linq;
using ZY.IBLL;
using ZY.IDAL;
using ZY.Model;

namespace ZY.BLL
{
    public class BaseBLL:IBaseBLL
    {
        IBaseDAL<UserModel> dal;
        IBaseDAL<ZTModel> zt;
        public BaseBLL(IBaseDAL<UserModel> dal, IBaseDAL<ZTModel> zt)
        {
            this.dal = dal;
            this.zt = zt;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> Add(UserModel t)
        {
            return await dal.GetAdd(t);
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="totalcount"></param>
        /// <param name="totalpage"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IQueryable<DTO> All(out int totalcount, out int totalpage, int pageindex = 1, int pagesize = 2)
        {
            var list = dal.GetAll().Join(zt.GetAll(), a => a.ZTid, b => b.Id, (a, b) => new DTO
            {
                Id = a.Id,
                Codr=a.Codr,
                Name = a.Name,
                Pwd = a.Pwd,
                DateTime = a.DateTime,
                Age = a.Age,
                Img = a.Img,
                IsAdmin = a.IsAdmin,
                ZTid = a.ZTid,
                Sex = a.Sex,
                ZTName = b.ZTName
            });
            totalcount = list.Count();
            totalpage = (int)Math.Ceiling(totalcount * 1.0 / pagesize);
            list = list.OrderBy(x => x.Id).Skip((pageindex - 1) * pagesize).Take(pagesize);
            return list;
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<UserModel> Find(int id)
        {
            return await dal.GetValue(x=>x.Id==id);
        }
        /// <summary>
        /// 状态表显示
        /// </summary>
        /// <returns></returns>
        public IQueryable<ZTModel> Show()
        {
            return zt.GetAll();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> Update(UserModel t)
        {
            return await dal.GetRemove(t);
        }

    }
}
